﻿using LPBToolsLib;
using StationNF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PLCConsole.PLCSerStation
{
    public class Station_WFDRVX : Station_PLCBase
    {
        public const ushort RegAddr_Input = 0x10;
        public const ushort RegAddr_Output = 0x11;
        public const ushort RegAddr_RunType = 0x12;
        public const ushort RegAddr_MotorSleepping = 0x13;
        public const ushort RegAddr_MotorSleepLong = 0x14;
        public const ushort RegAddr_MotorPowerOnTick = 0x15;
        public const ushort RegAddr_MotorPowerOnLong = 0x16;
        public const ushort RegAddr_AD1 = 0x17;
        public const ushort RegAddr_AD2 = 0x18;
        public const ushort RegAddr_Tempture = 0x19;
        public const ushort RegAddr_Humidity = 0x1A;
        public const int StationRegStar = 0x10;
        public const int StationRegCount = 11;

        public override bool isReady
        {
            get
            {
                if (base.isReady)
                {
                    return (StationRegStar == tSourStation.StationRegStar) &&
                        (StationRegCount == tSourStation.StationRegCount);
                }
                return false;
            }
        }

        public Station_WFDRVX(IFStation SourStation) : base(SourStation)
        {
            StationType = StationPLCType.WFDRVX;
        }

        public Station_WFDRVX(IFStationsServer SourStaServer, string DestStaID):base(SourStaServer, DestStaID)
        {
            StationType = StationPLCType.WFDRVX;
        }

        public bool Set_YPort(int iYIndex, bool iValues)
        {
            if ((tSourStation != null) && (iYIndex < 2) && (iYIndex >= 0))
            {
                DExOption_DownLoadWriteOn_0x05 tOptWrite0x05 = new DExOption_DownLoadWriteOn_0x05();
                tOptWrite0x05.YIndex = (byte)iYIndex;
                tOptWrite0x05.Value = iValues;
                tSourStation.setResponCommand(tOptWrite0x05);
                tSourStation.setPLCRunOnOff(iYIndex, iValues);    
            }
            return false;
        }

        public bool getXValue(int iXIndex)
        {
            if ((tSourStation != null) && (iXIndex >= 0) && (iXIndex < 2))
            {
                int iInputReg = tSourStation.RegValues[RegAddr_Input- StationRegStar];
                return ((iInputReg >> iXIndex) & 1) != 0;
            }
            return false;
        }
        public bool getYValue(int iYIndex)
        {
            if ((iYIndex >= 0) && (iYIndex < 2))
            {
                int iOutputReg = tSourStation.RegValues[RegAddr_Output - StationRegStar];
                return ((iOutputReg >> iYIndex) & 1) != 0;
            }
            return false;
        }

        public ushort RunType
        {
            get
            {
                return tSourStation.RegValues[RegAddr_RunType - StationRegStar];
            }
            set
            {
                if (tSourStation != null)
                {
                    DExOption_DownLoadWriteReg_0x10 tWriteReg0x10=new DExOption_DownLoadWriteReg_0x10(RegAddr_RunType,new ushort[] { value});
                    tSourStation.setResponCommand(tWriteReg0x10);
                }               
            }
        }

        public ushort AD1 { get { return tSourStation.RegValues[RegAddr_AD1 - StationRegStar]; } }
        public ushort AD2 { get { return tSourStation.RegValues[RegAddr_AD2 - StationRegStar]; } }
        public float Tempture
        {
            get
            {
                float iTemp = tSourStation.RegValues[RegAddr_Tempture - StationRegStar];
                return iTemp / 100;
            }
        }
        public float Humidity
        {
            get
            {
                float iTemp = tSourStation.RegValues[RegAddr_Humidity - StationRegStar];
                return iTemp / 100;
            }
        }
        public int MotorSleepping { get { return tSourStation.RegValues[RegAddr_MotorSleepping - StationRegStar]; } }
        public int MotorSleepLong { get { return tSourStation.RegValues[RegAddr_MotorSleepLong - StationRegStar]; } }
        public int MotorPowerOnTick { get { return tSourStation.RegValues[RegAddr_MotorPowerOnTick - StationRegStar]; } }
        public int MotorPowerOnLong { get { return tSourStation.RegValues[RegAddr_MotorPowerOnLong - StationRegStar]; } }       
    }
}
